############################################
## author:    Robert A. Huber
## contact:   robert.huber@sbg.ac.at
## file name: beliefs_descriptives.R
## Context:   Environmental Politics Paper on Beliefs
## started:   2018-10-04
## Summary:   read in csv files
##############################################

## empty memory (!)
rm(list=ls())

ri <- read.csv(file = "./final_data/ri.csv")
df_conj_out1 <- read.csv(file = "./final_data/df_conj_out1.csv")

rm(list=setdiff(ls(), c("ri", "df_conj_out1")))

sup <- c(ri$p1_su, ri$p2_su, ri$p3_su, ri$p4_su, ri$p5_su, ri$p6_su, ri$p7_su)
eff <- c(ri$p1_ef, ri$p2_ef, ri$p3_ef, ri$p4_ef, ri$p5_ef, ri$p6_ef, ri$p7_ef)
int <- c(ri$p1_in, ri$p2_in, ri$p3_in, ri$p4_in, ri$p5_in, ri$p6_in, ri$p7_in)
fai <- c(ri$p1_fa, ri$p2_fa, ri$p3_fa, ri$p4_fa, ri$p5_fa, ri$p6_fa, ri$p7_fa)
pol <- rep(c("Car Tax", "Env. Bonus", "Car Ban", "Parking Spaces",
         "Information Campaign", "Road Pricing", "Energy Label"), each = nrow(ri))
frame <- c(rep(ri$frame, 7))
id <- c(rep(ri$id, 7))

df_des <- data.frame(pol, sup, eff, int, fai, frame, id)
df_des$pol <- factor(df_des$pol, levels = c("Car Tax", "Env. Bonus", "Car Ban", "Parking Spaces", "Information Campaign", "Road Pricing", "Energy Label"), labels = c("Car Tax", "Env. Bonus", "Car Ban", "Parking \nSpaces", "Information \nCampaign", "Road Pricing", "Energy Label"))
df_des$sup <- factor(df_des$sup, levels = c(1:7), labels = c("Fully oppose", 2:6, "Fully support"))
df_des$eff <- factor(df_des$eff, levels = c(1:7), labels = c("Very effective", 2:6, "Very ineffective"))
df_des$int <- factor(df_des$int, levels = c(1:7), labels = c("Not at all intrusive", 2:6, "Very intrusive"))
df_des$fai <- factor(df_des$fai, levels = c(1:7), labels = c("Very unfair", 2:6, "Very fair"))
df_des$frame <- factor(df_des$frame, levels = c(1:2), labels = c("ECars", "Emissions"))

# Description of Explanations of beliefs ####

df_des <- merge(df_des, ri, by="id")

df_des$eff <- abs(as.numeric(df_des$eff)-8)
df_des$fai <- as.numeric(df_des$fai)
df_des$int <- as.numeric(df_des$int)
df_des$sup <- as.numeric(df_des$sup)

df_des$pol <- factor(df_des$pol, levels = c("Road Pricing", "Car Tax", "Env. Bonus",
                                            "Car Ban", "Parking \nSpaces", "Information \nCampaign",
                                            "Energy Label"))

m_eff <- lmer(eff ~ pol+ frame + age + I(age^2) + gender + edu + mp +  uar +  leftright + (1 | id), df_des)

m_int <- lmer(int  ~ pol+ frame +  age + I(age^2) + gender + edu + mp +  uar +  leftright+ (1|id), df_des)

m_fai <- lmer(fai ~ pol+ frame + age + I(age^2) + gender + edu + mp  +  uar +  leftright+ (1|id), df_des)

##### END DESCRIPTION #####

figureA1 <- ggplot(ri, aes(age)) +
  geom_density(aes(colour=gender), size = 1.5) +
  xlab("Age") +
  ylab("Density") +
  theme_bw() +
  facet_grid(frame~region7) +
  theme(strip.background = element_rect(fill = 'white')) +
  theme(text = element_text(size=14), axis.text.x = element_text(size=14)) + 
  scale_color_colorblind("Sex")
figureA1

figureA2 <- ggplot(ri, aes(uar)) +
  geom_bar(fill = "white", colour = "black") +
  xlab("Living Situation") +
  ylab("Count") +
  theme_bw() +
  theme(text = element_text(size=20), axis.text.x = element_text(size=16))
figureA2


rm(list=setdiff(ls(), c("ri", "df_conj_out1")))

rate <- c(ri$p1_ef, ri$p2_ef, ri$p3_ef, ri$p4_ef, ri$p5_ef, ri$p6_ef, ri$p7_ef,
          ri$p1_fa, ri$p2_fa, ri$p3_fa, ri$p4_fa, ri$p5_fa, ri$p6_fa, ri$p7_fa,
          ri$p1_in, ri$p2_in, ri$p3_in, ri$p4_in, ri$p5_in, ri$p6_in, ri$p7_in,
          ri$p1_su, ri$p2_su, ri$p3_su, ri$p4_su, ri$p5_su, ri$p6_su, ri$p7_su) 

dime <- c(rep(c("Effectiveness", "Fairness", "Intrusiveness", "Support"), each=7*nrow(ri)))
pol <- rep(rep(c("Car Tax", "Env. Bonus", "Car Ban", "Parking Spaces",
             "Information Campaign", "Road Pricing", "Energy Label"), each = nrow(ri)), 4)

frame <- rep(c(ri$frame), 28)
frame <- factor(frame, levels = c(2:1), labels = c("EV\nFrame", "Emission Reduction\nFrame"))
dime <- factor(dime, levels = c("Effectiveness", "Intrusiveness", "Fairness", "Support"))
df_loop <- data.frame(rate, dime, pol, frame)

sem <- function(x) sd(x)/sqrt(length(x))

out_bel <- df_loop %>%
  group_by(dime,frame, pol) %>%
  summarise(mean = mean(rate), n = n(), sem = sem(rate))

figure2 <- ggplot(subset(out_bel, out_bel$dime != "Support"), aes(y= mean, x=pol, group = frame))+
  geom_pointrange(aes(min = mean-1.95*sem, max = mean + 1.95*sem, shape = frame ),position=position_dodge(width=.75))+
  geom_hline(yintercept = 4, lty="dashed") +
  facet_grid(~dime)+
  xlab("Policy") +
  ylab("Perceived Consequences\nof Policies") +
  theme_bw() +
  scale_shape_manual(name = c("Frame"), values = c(15,16), labels = c("EV", "Emission\nReduction"))+
  theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) +
  theme(strip.background = element_rect(fill = 'white')) +
  theme(text = element_text(size=20), axis.text.x = element_text(size=16))
figure2

df_conj_out1 <- merge(df_conj_out1, ri, by = "id", prefixes = "")

df_conj_out1$attr_EN <- plyr::revalue(df_conj_out1$attrib1_lab, 
                                c("6) Road Pricing"="Road Pricing",
                                  "7) Energy Label"="Energy Label",
                                  "5) Information Campaign" = "Information Campaign",
                                  "4) Parking Spaces" = "Parking Spaces",
                                  "3) Car Ban" = "Car Ban",
                                  "1) Car Tax" = "Car Tax",
                                  "2) Env. Bonus" = "Environmental Bonus"))

#detach("package:plyr", unload=TRUE)
library(dplyr)

rm(list=setdiff(ls(), c("ri", "df_conj_out1")))

##END OF SCRIPT